package com.label.Web;

import com.label.Config.Logger.LogAnnotation;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Common.Validation.Del;
import com.label.Model.Common.Validation.Get;
import com.label.Model.Common.Validation.Save;
import com.label.Model.Common.Validation.Update;
import com.label.Model.Query.JobQuery;
import com.label.Service.JobService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/job")
public class JobController {

    @Resource
    private JobService jobService;

    @RequestMapping("/getAllJob")
    @LogAnnotation(module = "任务模块",operator = "获取所有任务")
    @RequiresAuthentication
    public ResponseBean getAllJob(){
        return new ResponseBean(200,"查找成功！",jobService.getAllJob());
    }

    @RequestMapping("/getOneJob")
    @LogAnnotation(module = "任务模块",operator = "获取一个任务")
    @RequiresAuthentication
    public ResponseBean getAllJob(@Validated(value = Get.class) @RequestBody JobQuery jobQuery){
        return new ResponseBean(200,"查找成功！",jobService.getOneById(jobQuery));
    }

    @RequestMapping("/getMyJob")
    @LogAnnotation(module = "任务模块",operator = "获取当前用户创建的任务")
    @RequiresAuthentication
    public ResponseBean getMyJob(){
        return new ResponseBean(200,"查找成功！",jobService.getMyJobs());
    }

    @RequestMapping("/getByState")
    @LogAnnotation(module = "任务模块",operator = "根据任务状态获取任务")
    @RequiresAuthentication
    public ResponseBean getByState(@RequestBody JobQuery jobQuery){
        return new ResponseBean(200,"查找成功！",jobService.getAllByStates(jobQuery));
    }

    @RequestMapping("/getAllByName")
    @LogAnnotation(module = "任务模块",operator = "根据名称获取任务")
    @RequiresAuthentication
    public ResponseBean getAllByName(@RequestBody JobQuery jobQuery){
        return new ResponseBean(200,"查找成功！",jobService.getAllByName(jobQuery));
    }

    @RequestMapping("/del")
    @LogAnnotation(module = "任务模块",operator = "删除任务")
    @RequiresAuthentication
    public ResponseBean del(@Validated(value = Del.class) @RequestBody JobQuery jobQuery){
        return new ResponseBean(200,"查找成功！",jobService.delJob(jobQuery));
    }

    @RequestMapping("/add")
    @LogAnnotation(module = "任务模块",operator = "添加任务")
    @RequiresAuthentication
    public ResponseBean add(@Validated(value = Save.class) @RequestBody JobQuery jobQuery){
        return new ResponseBean(200,"查找成功！",jobService.addJob(jobQuery));
    }

    @RequestMapping("/update")
    @LogAnnotation(module = "任务模块",operator = "更新任务信息")
    @RequiresAuthentication
    public ResponseBean update(@Validated(value = Update.class) @RequestBody JobQuery jobQuery){
        return new ResponseBean(200,"查找成功！",jobService.updateJob(jobQuery));
    }
}
